package com.sprucetec.live.controller;

import com.sprucetec.live.annotation.SystemControllerLog;
import com.sprucetec.live.common.dto.PagedListDTO;
import com.sprucetec.live.common.dto.business.LiveAnchorDto;
import com.sprucetec.live.common.dto.business.LiveCategoryDto;
import com.sprucetec.live.common.dto.business.LiveIntranetIpDto;
import com.sprucetec.live.common.dto.business.LiveUserDto;
import com.sprucetec.live.common.util.RespCode;
import com.sprucetec.live.common.util.ServiceResult;
import com.sprucetec.live.common.util.ServiceResultUtil;
import com.sprucetec.live.entity.LiveAnchor;
import com.sprucetec.live.entity.LiveCategory;
import com.sprucetec.live.entity.LiveIntranetIp;
import com.sprucetec.live.entity.LiveUser;
import com.sprucetec.live.service.LiveAnchorService;
import com.sprucetec.live.service.LiveCategoryService;
import com.sprucetec.live.service.LiveIntranetIpService;
import com.sprucetec.live.service.LiveUserService;
import com.sprucetec.live.util.SsoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName BasicController
 * @Description: 直播基础信息控制器
 * @Author Kenny
 * @Date 2020/3/30
 **/
@Slf4j
@RestController
@RequestMapping("/liveBasic")
public class LiveBasicController extends BaseController {

    @Autowired
    private LiveCategoryService liveCategoryService;

    @Autowired
    private LiveUserService liveUserService;

    @Autowired
    private LiveAnchorService liveAnchorService;

    @Autowired
    private LiveIntranetIpService liveIntranetIpService;

    /**
     * 分页查询分类列表
     *
     * @param liveCategoryDto
     * @return
     */
    @PostMapping("getCategoryPage")
    public ServiceResult<PagedListDTO<LiveCategoryDto>> getCategoryPage(@RequestBody LiveCategoryDto liveCategoryDto) {
        liveCategoryDto.judgePage();
        return ServiceResultUtil.success(liveCategoryService.getCategoryPage(liveCategoryDto));
    }

    /**
     * 创建分类
     */
    @SystemControllerLog(description = "新增分类")
    @PostMapping("createCategory")
    public ServiceResult<Integer> createCategory(@RequestBody LiveCategory liveCategory) {
        liveCategory.setId(null);
        String checkResult = liveCategoryService.checkCategory(liveCategory);
        if (checkResult != null) {
            return ServiceResultUtil.fail(RespCode.ERROR, checkResult);
        }
        return ServiceResultUtil.success(liveCategoryService.createCategory(liveCategory));
    }

    /**
     * 修改分类
     */
    @SystemControllerLog(description = "修改分类")
    @PostMapping("updateCategory")
    public ServiceResult<Integer> updateCategory(@RequestBody LiveCategory liveCategory) {
        if (liveCategory.getId() == null) {
            return ServiceResultUtil.fail(RespCode.ERROR, "参数错误：id不能为空");
        }
        String checkResult = liveCategoryService.checkCategory(liveCategory);
        if (checkResult != null) {
            return ServiceResultUtil.fail(RespCode.ERROR, checkResult);
        }
        return ServiceResultUtil.success(liveCategoryService.updateCategory(liveCategory));
    }

    /**
     * 获取所有分类集合
     */
    @PostMapping("getCategoryList")
    public ServiceResult<List<LiveCategory>> getCategoryList() {
        return ServiceResultUtil.success(liveCategoryService.getCategoryList());
    }


    /**
     * 分页查询用户列表
     */
    @PostMapping("getLiveUserPage")
    public ServiceResult<PagedListDTO<LiveUserDto>> getLiveUserPage(@RequestBody LiveUserDto liveUserDto) {
        liveUserDto.judgePage();
        return ServiceResultUtil.success(liveUserService.getLiveUserPage(liveUserDto));
    }

    /**
     * 用户禁言 解禁
     */
    @SystemControllerLog(description = "用户禁言解禁")
    @PostMapping("updateLiveUser")
    public ServiceResult<Integer> updateLiveUser(@RequestBody LiveUser liveUser) {
        /*if (liveUser.getId() == null) {
            return ServiceResultUtil.fail(RespCode.ERROR, "参数错误：id不能为空");
        }*/
        return ServiceResultUtil.success(liveUserService.updateLiveUser(liveUser));
    }

    /**
     * 分页查询主播列表
     *
     * @param liveAnchorDto
     * @return
     */
    @PostMapping("getLiveAnchorPage")
    public ServiceResult<PagedListDTO<LiveAnchor>> getLiveAnchorPage(@RequestBody LiveAnchorDto liveAnchorDto) {
        liveAnchorDto.judgePage();
        return ServiceResultUtil.success(liveAnchorService.getLiveAnchorPage(liveAnchorDto));
    }

    /**
     * 根据主播id，获取详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "getLiveAnchorInfo")
    public ServiceResult<LiveAnchor> getLiveAnchorInfo(@RequestParam(value = "id") Long id) {
        if (id == null) {
            return ServiceResultUtil.fail(RespCode.ERROR, "参数错误：id不能为空");
        }
        return ServiceResultUtil.success(liveAnchorService.getLiveAnchorInfo(id));
    }

    /**
     * 新增主播
     *
     * @param liveAnchor
     * @return
     */
    @SystemControllerLog(description = "新增主播")
    @PostMapping("addLiveAnchor")
    public ServiceResult<String> addLiveAnchor(@RequestBody LiveAnchor liveAnchor) {
        liveAnchorService.addLiveAnchor(liveAnchor);
        return ServiceResultUtil.success("添加成功");
    }

    /**
     * 修改主播
     *
     * @param liveAnchor
     * @return
     */
    @SystemControllerLog(description = "修改主播")
    @PostMapping("updateLiveAnchor")
    public ServiceResult<String> updateLiveAnchor(@RequestBody LiveAnchor liveAnchor) {
        if (liveAnchor.getId() == null) {
            return ServiceResultUtil.fail(RespCode.ERROR, "参数错误：id不能为空");
        }
        liveAnchorService.updateLiveAnchor(liveAnchor);
        return ServiceResultUtil.success("修改成功");
    }

    /**
     * 获取主播集合
     *
     * @return
     */
    @PostMapping("getLiveAnchorList")
    public ServiceResult<List<LiveAnchor>> getLiveAnchorList() {
        return ServiceResultUtil.success(liveAnchorService.getLiveAnchorList());
    }


    /**
     * 获取内网IP
     *
     * @return
     */
    @PostMapping("getLiveIpList")
    public ServiceResult<PagedListDTO<LiveIntranetIp>> getLiveIpList(@RequestBody LiveIntranetIpDto liveIntranetIpDto) {
        liveIntranetIpDto.judgePage();
        return ServiceResultUtil.success(liveIntranetIpService.getLiveIpListPage(liveIntranetIpDto));
    }

    /**
     * 添加内网IP
     *
     * @return
     */
    @SystemControllerLog(description = "添加内网IP")
    @PostMapping("addLiveIp")
    public ServiceResult<String> addLiveIp(@RequestBody LiveIntranetIp liveIntranetIp) {
        liveIntranetIp.setCreateTime(new Date());
        liveIntranetIp.setCreateUserName(SsoUtil.getLoginUser().getName());
        liveIntranetIpService.addLiveIp(liveIntranetIp);
        return ServiceResultUtil.success("添加成功");
    }


    @SystemControllerLog(description = "修改内网IP")
    @PostMapping("updateLiveIp")
    public ServiceResult<String> updateLiveIp(@RequestBody LiveIntranetIp liveIntranetIp) {
        if (liveIntranetIp.getId() == null) {
            return ServiceResultUtil.fail(RespCode.ERROR, "参数错误：id不能为空");
        }
        liveIntranetIp.setUpdateTime(new Date());
        liveIntranetIp.setUpdateUserName(SsoUtil.getLoginUser().getName());
        liveIntranetIpService.updateLiveIp(liveIntranetIp);
        return ServiceResultUtil.success("修改成功");
    }

    @SystemControllerLog(description = "删除内网IP")
    @GetMapping("deleteLiveIp")
    public ServiceResult<String> deleteLiveIp(@RequestParam("id") Long id) {
        liveIntranetIpService.deleteLiveIp(id);
        return ServiceResultUtil.success("修改成功");
    }
}
